package stz.flash.main.flashlight.field;

import stz.flash.Info;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.RealtimeClockListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import blackberry.util.image.DjImageCalculator;
import blackberry.util.image.DjImageResizer;

public class BatteryManager extends VerticalFieldManager implements RealtimeClockListener {
	
	private static final int HEIGHT_BATTERY_IMAGE = Info.PX_OF_2MM;
	private static final int WIDTH_BATTERY_IMAGE = DjImageCalculator.extractWidthFromHeight(
			44, 
			25, 
			HEIGHT_BATTERY_IMAGE);
		
	private final BitmapField IMAGE_BATTERY_100 = new BitmapField( 
			DjImageResizer.fitIntoNewBackground( 
					Bitmap.getBitmapResource("battery100.png"), 
					44, 25, 
					44, 25,
					WIDTH_BATTERY_IMAGE, HEIGHT_BATTERY_IMAGE, 
					0, Bitmap.FILTER_BILINEAR));
	
	private final BitmapField IMAGE_BATTERY_90 = new BitmapField( 
			DjImageResizer.fitIntoNewBackground( 
					Bitmap.getBitmapResource("battery90.png"), 
					44, 25, 
					44, 25,
					WIDTH_BATTERY_IMAGE, HEIGHT_BATTERY_IMAGE, 
					0, Bitmap.FILTER_BILINEAR));
	
	private final BitmapField IMAGE_BATTERY_70 = new BitmapField( 
			DjImageResizer.fitIntoNewBackground( 
					Bitmap.getBitmapResource("battery70.png"), 
					44, 25, 
					44, 25,
					WIDTH_BATTERY_IMAGE, HEIGHT_BATTERY_IMAGE, 
					0, Bitmap.FILTER_BILINEAR));
	
	private final BitmapField IMAGE_BATTERY_50 = new BitmapField( 
			DjImageResizer.fitIntoNewBackground( 
					Bitmap.getBitmapResource("battery50.png"), 
					44, 25, 
					44, 25,
					WIDTH_BATTERY_IMAGE, HEIGHT_BATTERY_IMAGE, 
					0, Bitmap.FILTER_BILINEAR));
	
	private final BitmapField IMAGE_BATTERY_30 = new BitmapField( 
			DjImageResizer.fitIntoNewBackground( 
					Bitmap.getBitmapResource("battery30.png"), 
					44, 25, 
					44, 25,
					WIDTH_BATTERY_IMAGE, HEIGHT_BATTERY_IMAGE, 
					0, Bitmap.FILTER_BILINEAR));
	
	private final BitmapField IMAGE_BATTERY_10 = new BitmapField( 
			DjImageResizer.fitIntoNewBackground( 
					Bitmap.getBitmapResource("battery10.png"), 
					44, 25, 
					44, 25,
					WIDTH_BATTERY_IMAGE, HEIGHT_BATTERY_IMAGE, 
					0, Bitmap.FILTER_BILINEAR));
	
	
	private HorizontalFieldManager imageManager = new HorizontalFieldManager();
	private BatteryLabel percentLabel = new BatteryLabel("", FIELD_VCENTER);
	private BitmapField currentBatteryImage;
	
	
	public BatteryManager() {
		
		Application.getApplication().addRealtimeClockListener(this);
		
		//베터리 그림 업데이트시 순서가 바뀌지 않도록 하기위해 상대적으로 복잡하게 처리함
		HorizontalFieldManager batteryManager = new HorizontalFieldManager();
		batteryManager.add(imageManager);
		batteryManager.add(percentLabel);
		add(batteryManager);
		
		clockUpdated();
	}
	
	public void clockUpdated() {
		int batteryLevel = DeviceInfo.getBatteryLevel();
		updateBatteryLabel(batteryLevel);
		updateBaterryImage(batteryLevel);
	}

	private void updateBatteryLabel(int batteryLevel) {
		synchronized (UiApplication.getEventLock()) {
			percentLabel.setText(" " + batteryLevel + "%");
		}
	}
	
	private void updateBaterryImage(int batteryLevel){
		if (batteryLevel >= 90) {
			replaceImage(IMAGE_BATTERY_100);
			currentBatteryImage = IMAGE_BATTERY_100;
		} else if (batteryLevel >= 70) {
			replaceImage(IMAGE_BATTERY_90);
			currentBatteryImage = IMAGE_BATTERY_90;
		} else if (batteryLevel >= 50) {
			replaceImage(IMAGE_BATTERY_70);
			currentBatteryImage = IMAGE_BATTERY_70;
		} else if (batteryLevel >= 30) {
			replaceImage(IMAGE_BATTERY_50);
			currentBatteryImage = IMAGE_BATTERY_50;
		} else if (batteryLevel >= 10) {
			replaceImage(IMAGE_BATTERY_30);
			currentBatteryImage = IMAGE_BATTERY_30;
		} else if (batteryLevel < 10) {
			replaceImage(IMAGE_BATTERY_10);
			currentBatteryImage = IMAGE_BATTERY_10;
		}
	}
	
	private void replaceImage(BitmapField newBatteryImage){
		if (currentBatteryImage == newBatteryImage) 
			return;

		if (currentBatteryImage != null && currentBatteryImage.getIndex() != -1) 
			imageManager.delete(currentBatteryImage);
		
		imageManager.add(newBatteryImage);
	}
	
	class BatteryLabel extends ColorChangeLabel {
		
		public BatteryLabel(Object label, long style) {
			super(label, style);
			setFont();
		}

		private void setFont(){
			int fontHeight = HEIGHT_BATTERY_IMAGE;
			setFont(getFont().derive(Font.ITALIC, fontHeight));
		}
		
		//percentLabel이 이미지와 세로 관계에 있을때
		//		private void setFont(){
		//			int fontHeight = FontCalculater.getFontHeightIntoAdvance( 
		//					"100%", 
		//					getFont(), 
		//					IMAGE_BATTERY_100.getPreferredWidth());
		//			
		//			setFont(getFont().derive(Font.ITALIC, fontHeight));
		//		}
	}
}
